package com.ququ.syb.entrance.service;

import com.ququ.common.result.Result;
import com.ququ.syb.common.constant.EncryptTypeConstant;
import com.ququ.syb.common.constant.ResultStatusConstant;
import com.ququ.syb.common.pojo.BankAccountInfo;
import com.ququ.syb.common.pojo.PayBankAccountInfo;
import com.ququ.syb.common.pojo.PayResult;
import com.ququ.syb.entrance.fegin.BankCardFegin;
import com.ququ.syb.entrance.po.RequestData;
import com.ququ.syb.entrance.vo.SybResult;
import com.ququ.syb.entrance.vo.SybResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class BankCardService {
    @Autowired
    private BankCardFegin bankCardFegin;
    @Autowired
    private SecurityService securityService;

    /**
     * 绑定提现卡 p1308接口实现
     * @param requestData
     * @return
     */
    public SybResult bindBankCard(RequestData<String> requestData) {
        if (requestData.getEncryptType() != EncryptTypeConstant.AES)
            return new SybResult(-1, "加密方式不正确", securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String, Object> dataMap = securityService.getParametersMap(requestData);
        String accountid = requestData.getAccountid();
        String appcode = requestData.getAppcode();
        if (!dataMap.containsKey("bankaccountno") || dataMap.get("bankaccountno")==null )
            return new SybResult(-1,"缺少银行卡号",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String bankaccountno = dataMap.get("bankaccountno").toString().replaceAll("\t| |\r|\n|-|\\s|\\uFEFF","");
        if (!dataMap.containsKey("bankaccountname") || dataMap.get("bankaccountname")==null )
            return new SybResult(-1,"缺少姓名参数",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String bankaccountname = dataMap.get("bankaccountname").toString().replaceAll("\t| |\r|\n|-|\\s|\\uFEFF","");
        if (!dataMap.containsKey("cardholdermobile") || dataMap.get("cardholdermobile")==null )
            return new SybResult(-1,"缺少手机号",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String cardholdermobile = dataMap.get("cardholdermobile").toString().replaceAll("\t| |\r|\n|-|\\s|\\uFEFF","");
        if (!dataMap.containsKey("cardholderidcard") || dataMap.get("cardholderidcard")==null )
            return new SybResult(-1,"缺少身份证号",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String cardholderidcard = dataMap.get("cardholderidcard").toString().replaceAll("\t| |\r|\n|-|\\s|\\uFEFF","").toUpperCase();
        if (!dataMap.containsKey("bankprovince") || dataMap.get("bankprovince")==null )
            return new SybResult(-1,"缺少开户行省份参数",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String bankprovince = dataMap.get("bankprovince").toString();
        if (!dataMap.containsKey("bankcity") || dataMap.get("bankcity")==null )
            return new SybResult(-1,"缺少开户行城市参数",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String bankcity = dataMap.get("bankcity").toString();
        if (!dataMap.containsKey("branchno") || dataMap.get("branchno")==null )
            return new SybResult(-1,"缺少开户行联行号",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String branchno = dataMap.get("branchno").toString();

        Result<BankAccountInfo> bankAccountInfoResult = bankCardFegin.bindBankAccountInfo(accountid,bankaccountno,bankaccountname,cardholdermobile,cardholderidcard,bankprovince,bankcity,branchno);
        if (bankAccountInfoResult.getStatus() == ResultStatusConstant.SUCCESS)
            return securityService.buildSybResult(bankAccountInfoResult.getData(),requestData);
        else
            return new SybResult(-1,bankAccountInfoResult.getMsg(),securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
    }


    /**
     * 查询本账户绑定的提现卡  p1303接口
     * @param requestData
     * @return
     */
    public SybResult getBankCard(RequestData<String> requestData) {
        if (requestData.getEncryptType() != EncryptTypeConstant.AES)
            return new SybResult(-1, "加密方式不正确", securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String, Object> dataMap = securityService.getParametersMap(requestData);
        String accountid = requestData.getAccountid();
        Result<BankAccountInfo> bankAccountInfoResult = bankCardFegin.getBankAccountInfo(accountid);
        if (bankAccountInfoResult.getStatus() == ResultStatusConstant.SUCCESS)
            return securityService.buildSybResult(bankAccountInfoResult.getData(),requestData);
        else
            return new SybResult(-1,bankAccountInfoResult.getMsg(),securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
    }


    /**
     * 绑定支付卡 p1321接口
     * @param requestData
     * @return
     */
    public SybResult bindPayBankCard(RequestData<String> requestData) {
        if (requestData.getEncryptType() != EncryptTypeConstant.AES)
            return new SybResult(-1, "加密方式不正确", securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String, Object> dataMap = securityService.getParametersMap(requestData);
        String accountid = requestData.getAccountid();
        String appcode = requestData.getAppcode();
        if (!dataMap.containsKey("bankaccountno") || dataMap.get("bankaccountno")==null )
            return new SybResult(-1,"缺少银行卡号",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String bankaccountno = dataMap.get("bankaccountno").toString().replaceAll("\t| |\r|\n|-|\\s|\\uFEFF","");
        if (!dataMap.containsKey("bankaccountname") || dataMap.get("bankaccountname")==null )
            return new SybResult(-1,"缺少姓名参数",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String bankaccountname = dataMap.get("bankaccountname").toString().replaceAll("\t| |\r|\n|-|\\s|\\uFEFF","");
        if (!dataMap.containsKey("cardholdermobile") || dataMap.get("cardholdermobile")==null )
            return new SybResult(-1,"缺少手机号",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String cardholdermobile = dataMap.get("cardholdermobile").toString().replaceAll("\t| |\r|\n|-|\\s|\\uFEFF","");
        if (!dataMap.containsKey("cardholderidcard") || dataMap.get("cardholderidcard")==null )
            return new SybResult(-1,"缺少身份证号",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String cardholderidcard = dataMap.get("cardholderidcard").toString().replaceAll("\t| |\r|\n|-|\\s|\\uFEFF","").toUpperCase();

        Result<PayBankAccountInfo> payBankAccountInfoResult = bankCardFegin.bindPayBankAccountInfo(accountid,bankaccountno,bankaccountname,cardholdermobile,cardholderidcard);
        if (payBankAccountInfoResult.getStatus() == ResultStatusConstant.SUCCESS)
            return securityService.buildSybResult(payBankAccountInfoResult.getData(),requestData);
        else
            return new SybResult(-1,payBankAccountInfoResult.getMsg(),securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
    }

}
